"""
Purpose: Computing, measuring, and visualize sequence similarity of different proteins via R package "protr"

Author : H. Lin, Ph.D., https://orcid.org/0000-0003-4060-7336 

This script contained R programming codes

Version: Created on May 2020, and updated on 25th Feb. 2025 and 18th Aug. 2025 (Lastest)


1. Project Title:
   Measuring amino acid sequences' similarities between different proteins using the R package "protr"


2. Project Background :
   Proteins are biomolecules with complex and complicated 3-Dimension structure.
 
   A sequence of amino acid forms the basic structure of protein, and the amino acid sequence is known as the primary structure of a protein. 
 
   Meansuring similarities or differences/distances between different proteins plays a very basic and important role in biomedical studies.
 
   For instance, the similarity measurement analysis of protein molecules helps down-stream analysis of Evolutionary Biology, compound-protein interactions, molecular/genomic/proteomic functional studies, and a variety of quantitative analyses of proteins, biomolecules, chemical molecules and drugs.  
 
   Generally (but not always true), When the measured similarity score of a pair of proteins is high, it could be considered that the protein is similar to each other, and the pair of proteins may have similar structure (secondary/tertiary) and functions to each other. 

   Otherwise, that protein pair should be considered dissimilar or distant to each other. 


3. Research Purpose(s)
	To use those example amino acid sequence data of proteins provided in this project tutorial, so as to comprehensively measure the sequence-based pair-wise primary distances and similarities of proteins.

 

"""
 #  To installation the package required for protein sequence computing
install.packages("protr")
 
 # To load the pack into R computing environment
library(protr)

# (1) Data preparation 
# A sample sequence data file ""protseq/Plasminogen.fasta"" was embedded and provided in the protr pack. We could call and use this sequence data as the example for this exercise.
seq <- readFASTA(system.file("protseq/Plasminogen.fasta", package = "protr"))  

# Or you could download your desired protein sequence file(s) into local computer disk, and then read/load your protein sequences (in FASTA format) into your R computing environment via the command/code below:
fs <- readFASTA("Fasta_file_path")


# Check the number of amino acids of the sequence 
length(seq) # 50 protein/amino acid sequences were stored in the "seq"



# (2) To embed / vectorize protein sequences. 
# To examplify, here only the amino acids composition descriptor function "extractAAC( )" was chosen and tested. Other kinds of protein descritpor / embedding functions are available, as well, but were not examplified here. For more details please refer to the documents of the package "protr"

# Note that the function extractAAC( ...) only recognizes those frequently seen 20 types of amino acids. If extract types of amino acids are in the sequence, error would happen when using this function extracAAC(...) .

# Construct a loop, and vectorize 50 amino acid sequences in the loop

# Create a matrix variable for storing vectors generated inside the loop. 50 is the number of sequence in the "seq" variable, while 20 is the dimension of vector generated through function "extractAAC(...)"
mat <- matrix(NA, 50, 20)

# The loop
for(i in 1:length(seq)){ mat[i] <- extractAAC(seq[[i]]) } 

# To examine if above embedding (vector generation and assignment) operation was successful.
print(mat) 



# (3) To compute the distance value between a pair of proteins, in order to reflect the sequence similarity. 
# Note that multiple types of distance computation methods are available to measure the distances between protein feature vectors. E.g., the Euclidean distance, the Manhattan distance, etc.
# Example: here I only take the default method of the function dist( ), i.e., the Euclidean distance as an example for computation.
# Note that, rbind( ) function may be used to add your desired protein vectors into the matrix for further computation.
dm <- dist(mat)



# (4) Visualize the distance matrix data through plotting a heatmap 
heatmap(as.matrix(dm), Colv=NA, Rowv=NA)


# For further details, refer to the "protr" package and documents in CRAN https://cran.r-rpoject.org/web/packages/protr/ and the research paper Xiao, N. et al. (2015) Predicting proten-protein interactions based only on seqeuences information. Proceedings of the National Academy of Sciences, 104, 4337-4341.

